package com.ztgf.order.service.purchase;

import com.ztgf.common.constant.CommonParameter;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.db.utils.RedisUtil;
import com.ztgf.dubbo.exception.ServiceException;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.sys.models.dto.SysUserDTO;
import com.ztgf.sys.models.service.ISysUserService;
import com.ztgf.user.models.dto.UserInfoDTO;
import com.ztgf.user.models.service.IUserInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.order.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.order.mapper.purchase.PurchaseCommodityLeaveMessageReplyMapper;
import com.ztgf.order.models.service.purchase.IPurchaseCommodityLeaveMessageReplyService;
import com.ztgf.order.models.bo.purchase.PurchaseCommodityLeaveMessageReplySaveBO;
import com.ztgf.order.models.dto.purchase.PurchaseCommodityLeaveMessageReplyDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 询价留言回复表控制器
 *
 * @author mxli
 * @Date 2021-08-24 15:43:14
 */
@DubboService
public class PurchaseCommodityLeaveMessageReplyServiceImpl extends ServiceImpl<PurchaseCommodityLeaveMessageReplyMapper, PurchaseCommodityLeaveMessageReplyDTO> implements IPurchaseCommodityLeaveMessageReplyService{

    @Resource
    private YmlConfig ymlConfig;
    @DubboReference
    private IShopInfoService shopInfoService;
    @DubboReference
    private IUserInfoService userInfoService;
    @DubboReference
    private ISysUserService sysUserService;
    @Autowired
    private RedisUtil redisUtil;
    @Override
    @Transactional(rollbackFor= ServiceException.class)
    public boolean add(PurchaseCommodityLeaveMessageReplySaveBO saveBO) {
        PurchaseCommodityLeaveMessageReplyDTO dto = new PurchaseCommodityLeaveMessageReplyDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        if (saveBO.getPublishUserType()!=2) {
            ShopInfoDTO shopInfoDTO = shopInfoService.selectById(saveBO.getShopId());
            if (shopInfoDTO != null) {
                dto.setShopName(shopInfoDTO.getShopName());
            }
        }
        if (saveBO.getReplyedPublishUserType()!=2) {
            ShopInfoDTO shopInfoDTO1 = shopInfoService.selectById(saveBO.getReplyedShopId());
            if (shopInfoDTO1 != null) {
                dto.setReplyedShopName(shopInfoDTO1.getShopName());
            }
        }
        if(saveBO.getCreateUserId() == null){
            Long userId = null;
            String userName = "";
            Object object = redisUtil.getSession(CommonParameter.getHeadParamsBO ().getSessionId (), CommonParameter.getHeadParamsBO ().getSource ().toString ());
            if(object == null){
                throw new ServiceException(CodeEnum.FAIL_100002);
            }
            if(object instanceof UserInfoDTO){
                userId = ((UserInfoDTO) object).getId ();
                userName = ((UserInfoDTO) object).getName ();
            } if(object instanceof SysUserDTO){
                userId = ((SysUserDTO) object).getId ();
                userName = ((SysUserDTO) object).getName ();
            }
            dto.setCreateUserId(userId);
            dto.setCreateUserName(userName);
        }
        if (saveBO.getPublishUserType()==0){
            dto.setShopName(dto.getCreateUserName());
        }
        if (saveBO.getReplyedPublishUserType()==0){
            ShopInfoDTO shopInfoDTO1 = shopInfoService.selectById(saveBO.getReplyedShopId());
            SysUserDTO sysUserDTO = sysUserService.selectById(shopInfoDTO1.getUserId());
            if (sysUserDTO!=null){
                dto.setReplyedShopName(sysUserDTO.getName());
            }
            UserInfoDTO userInfoDTO = userInfoService.selectById(shopInfoDTO1.getUserId());
            if (userInfoDTO!=null){
                dto.setReplyedShopName(userInfoDTO.getName());
            }
        }
        //如果是平台
        if (saveBO.getPublishUserType()==2){
            dto.setShopName(dto.getCreateUserName());
            dto.setShopId(dto.getCreateUserId());
        }
        if(saveBO.getReplyedPublishUserType()==2){
            SysUserDTO sysUserDTO = sysUserService.selectById(saveBO.getReplyedShopId());
            dto.setReplyedShopName(sysUserDTO.getName());
        }
        dto.setStatus(0);
        return this.insert(dto);
    }
}
